package org.je.database.impl;

import org.je.util.CharUtil;

/**
 * @author fanyifeng
 */
public class SqlChecker
{
	public static boolean isSelect(String sql) {
		if (sql == null) {
			throw new NullPointerException("sql-is-null");
		}
		int notBlank = -1;
		for (int i = 0; i < sql.length(); ++i) {
			if (CharUtil.isBlank(sql.charAt(i)) == false) {
				notBlank = i;
				break;
			}
		}
		if (notBlank == -1 || notBlank + 6 >= sql.length()) {
			throw new IllegalArgumentException("sql-error: " + sql);
		}

		return matchIgnoreCase(sql, "select", notBlank);
	}

	private static boolean matchIgnoreCase(String src, String token, int off) {
		for (int i = 0; i < token.length(); ++i) {
			if (CharUtil.equalsIgnoreCase(token.charAt(i), src.charAt(off + i)) == false) {
				return false;
			}
		}
		return true;
	}
}
